package com.lineage.data.item_etcitem;

import com.lineage.data.cmd.CreateNewItem;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ResolventTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;

public class Dissolution extends ItemExecutor {
  public static ItemExecutor get() {
    return new Dissolution();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int itemobj = data[0];
    L1ItemInstance item2 = pc.getInventory().getItem(itemobj);
    if (item2 == null)
      return; 
    useResolvent(pc, item2, item);
  }
  
  private void useResolvent(L1PcInstance pc, L1ItemInstance item, L1ItemInstance resolvent) {
    Random _random = new Random();
    if (item == null || resolvent == null) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (item.getItem().getType2() == 1 || item.getItem().getType2() == 2) {
      if (item.getEnchantLevel() != 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1161));
        return;
      } 
      if (item.isEquipped()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1161));
        return;
      } 
    } 
    long crystalCount = ResolventTable.get().getCrystalCount(item.getItem().getItemId());
    if (crystalCount == 0L) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1161));
      return;
    } 
    int rnd = _random.nextInt(100) + 1;
    if (rnd >= 1 && rnd <= 50) {
      crystalCount *= 1L;
    } else if (rnd >= 51 && rnd <= 90) {
      crystalCount = (long)(crystalCount * 1.5D);
    } else if (rnd >= 91 && rnd <= 100) {
      crystalCount *= 2L;
    } 
    if (crystalCount != 0L)
      CreateNewItem.createNewItem(pc, 41246, crystalCount); 
    pc.getInventory().removeItem(item, 1L);
    pc.getInventory().removeItem(resolvent, 1L);
  }
}
